package com.brsya.movie.presenter;

import com.alibaba.fastjson.JSON;
import com.brsya.base.base.BasePresenter;
import com.brsya.base.bean.BaseObjectBean;
import com.brsya.base.bean.PlayAddressBean;
import com.brsya.base.bean.UpdateBean;
import com.brsya.base.net.MyObserver;
import com.brsya.base.net.RxScheduler;
import com.brsya.base.util.LoggerUtil;
import com.brsya.movie.BuildConfig;
import com.brsya.movie.application.MyApplication;
import com.brsya.movie.contract.UpdateContract;
import com.brsya.movie.model.UpdateModel;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;


/**
 * Created by Brsya
 * CreateDate: 2022/4/29  18:04
 * Description:
 * 版本更新
 */
public class UpdatePresenter extends BasePresenter<UpdateContract.View> implements UpdateContract.Presenter {
    private final UpdateContract.Model model;

    public UpdatePresenter() {
        model = new UpdateModel();
    }

    @Override
    public void getUpdateLog() {
        Map<String, Object> par = getPublicPar();

        model.getUpdateLog(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseObjectBean<UpdateBean>>(mView) {
                    @Override
                    public void success(BaseObjectBean<UpdateBean> baseBean) {
                        baseBean.handleData(UpdateBean.class);

                        LoggerUtil.e("更新接口" + JSON.toJSONString(baseBean.getData()));
                        if (baseBean.getData() == null) {
                            return;
                        }
                        int versionNumber = baseBean.getData().getVersionNumber();
                        if (versionNumber > BuildConfig.VERSION_CODE) {
                            mView.showUpdateDialog(baseBean.getData());
                        }
                    }
                });
    }

    @Override
    public void getPlayAddressNew() {
        model.getPlayAddressNew(getPublicPar())
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseObjectBean<PlayAddressBean>>(mView) {
                    @Override
                    public void success(@NonNull BaseObjectBean<PlayAddressBean> baseBean) {
                        baseBean.handleData(PlayAddressBean.class);
                        MyApplication.playAddressBean = baseBean.getData();
                    }
                });
    }

}
